import time

from selenium import webdriver
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.PageLocate.czyl_user_locator import LoginlocatorCzyl

from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from Case.python_ui.Common.do_mysql import DoMysql
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.PageLocate.organization_locator import  OrganizationLocator
from Case.python_ui.PageLocate.admin_pu_organization_locator import AdminPUUserOrgLocator


class  Loginpages(Basepage):


    def user_login(self,cmdopt):
        doc = 'web货主登陆'
        self.driver.get(Commondata.url[cmdopt]['user'])
        self.wait_elevisble(LoginlocatorCzyl.login_register, doc, timeout=10)
        self.click_element(LoginlocatorCzyl.login_register, doc)
        self.wait_elevisble(LoginlocatorCzyl.down_click, doc, timeout=5)
        self.click_element(LoginlocatorCzyl.down_click,doc)
        self.wait_elevisble(LoginlocatorCzyl.choose_consignor, doc, timeout=5)
        self.click_element(LoginlocatorCzyl.choose_consignor,doc)
        self.send_keys(LoginlocatorCzyl.input_phone,Logindata.user_data[cmdopt]['user']['username'],doc)
        self.send_keys(LoginlocatorCzyl.input_password,Logindata.user_data[cmdopt]['user']['password'],doc)
        self.click_element(LoginlocatorCzyl.login_button,doc)
        self.wait_elevisble(LoginlocatorCzyl.check_login, doc, timeout=10)
        self.save_screen(doc)



        time.sleep(10)

    def login(self,cmdopt):
        doc='user_admin login'
        for i in range(3):
            self.driver.get(Commondata.url[cmdopt]['user'])
            try:
                #time.sleep(10)
                self.wait_elevisble(Loginlocator.login_username_ele, doc,timeout=10)
                self.wait_elevisble(Loginlocator.login_password_ele, doc,timeout=10)
                break
            except:
                continue
        #验证码
        #self.wait_elevisble(Loginlocator.verification_code,doc)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele,Logindata.user_data[cmdopt]['user']['username'],doc)
        self.send_keys(Loginlocator.login_password_ele,Logindata.user_data[cmdopt]['user']['password'],doc)
        #验证码
        #self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        self.wait_elevisble(Loginlocator.login_submit_ele,doc)
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele,doc)
        #self.wait_elevisble(Loginlocator.email_verification_code,doc)
        #self.click_element(Loginlocator.send_code_ele)
        #self.send_keys(Loginlocator.email_verification_code,Logindata.verification_code[cmdopt]['code'],doc)
        time.sleep(2)
        #self.click_element(Loginlocator.email_submit_ele,doc)
        #self.wait_elevisble(Loginlocator.dshboard_ele,doc)
        login_success=self.get_element(Loginlocator.login_success_ele, doc).text
        print(login_success)
        return login_success

    def user_register_before(self,cmdopt):
        doc='user register'
        self.driver.get(Commondata.url[cmdopt]['user'])
        time.sleep(10)
        self.wait_elevisble(Loginlocator.click_register,doc)
        self.move_mouse_to_click_element(Loginlocator.click_register,doc)
        time.sleep(3)
        self.wait_elevisble(Loginlocator.disabled_send,doc)
        send = self.get_element(Loginlocator.disabled_send,doc)
        print('send:', send.get_attribute('disabled'))
        assert send.get_attribute('disabled') == 'true'
        time.sleep(3)
        self.wait_elevisble(Loginlocator.input_code, doc)
        input = self.get_element(Loginlocator.input_code,doc)
        print('input:', input.get_attribute('placeholder'))
        assert input.get_attribute('placeholder') == "Click 'Send' to receive verification code"

    def user_register_after(self,cmdopt):
        doc='user register'
        self.driver.get(Commondata.url[cmdopt]['user'])
        time.sleep(10)
        self.wait_elevisble(Loginlocator.click_register,doc)
        self.move_mouse_to_click_element(Loginlocator.click_register,doc)
        time.sleep(3)
        self.wait_elevisble(Loginlocator.input_email, doc)
        self.send_keys(Loginlocator.input_email, '123@qq.com', doc)
        time.sleep(3)
        self.wait_elevisble(Loginlocator.click_send,doc)
        self.move_mouse_to_click_element(Loginlocator.click_send, doc)
        self.wait_elevisble(Loginlocator.countdown, doc)
        assert self.get_element(Loginlocator.countdown, doc).text == '90'
        time.sleep(3)
        self.wait_elevisble(Loginlocator.input_code, doc)
        input = self.get_element(Loginlocator.input_code,doc)
        print('input:', input.get_attribute('placeholder'))
        assert input.get_attribute('placeholder') == "6-digit verification code"





    def login_by_other_acct(self, cmdopt,client,acct):
        doc = '新账号登录'
        self.driver.get(Commondata.url[cmdopt][client])
        time.sleep(10)
        self.wait_elevisble(Loginlocator.login_username_ele, doc)
        self.wait_elevisble(Loginlocator.login_password_ele, doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code,doc)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data[cmdopt][acct]['username'], doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.user_data[cmdopt][acct]['password'], doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele, doc)
        self.wait_elevisble(Loginlocator.email_verification_code, doc)
        # self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code[cmdopt]['code'], doc)
        self.click_element(Loginlocator.email_submit_ele, doc)
        self.wait_elevisble(Loginlocator.dshboard_ele, doc)
        login_success = self.get_element(Loginlocator.login_success_ele, doc).text
        print(login_success)
        return login_success


    def receive_sms_opt(self,cmdopt,get_database_source):
        doc='login receive sms'
        self.driver.get(Commondata.url[cmdopt]['user'])
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data[cmdopt]['user4']['username'],doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.user_data[cmdopt]['user4']['password'],doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        # self.click_element(Loginlocator.login_submit_ele,doc)
        # self.wait_elevisble(Loginlocator.email_verification_code,doc)
        # self.click_element(Loginlocator.send_code_ele,doc)
        # time.sleep(5)
        # code=BusinessCommonTools().get_verification_code(cmdopt,Logindata.user_data[cmdopt]['user4']['username'],get_database_source)
        # self.send_keys(Loginlocator.opt_code_input_ele, code,doc)
        # self.click_element(Loginlocator.email_submit_ele,doc)
        # self.wait_elevisble(Loginlocator.dshboard_ele, doc)
        # login_success = self.get_element(Loginlocator.login_success_ele, doc).text
        # print(login_success)
        # return login_success

    def register_login(self,email,cmdopt):
        doc='user register login portal'
        self.driver.get(Commondata.url[cmdopt]['user'])
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        #验证码
        #self.wait_elevisble(Loginlocator.verification_code)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele,email,doc)
        self.send_keys(Loginlocator.login_password_ele,Logindata.new_pwd,doc)
        #验证码
        #self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele,doc)
        time.sleep(3)
        self.wait_elevisble(Loginlocator.email_verification_code,doc)
        #self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code,Logindata.verification_code[cmdopt]['code'],doc)
        self.click_element(Loginlocator.email_submit_ele,doc)
        time.sleep(10)
        login_success = self.get_element(Loginlocator.login_success_ele, doc).text
        print(login_success)
        return login_success


    def admin_register_login(self,email,cmdopt):
        doc = 'user register login portal'
        self.driver.get(Commondata.url[cmdopt]['admin'])
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele, doc)
        self.wait_elevisble(Loginlocator.login_password_ele, doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, email, doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.new_pwd, doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele, doc)
        time.sleep(3)
        self.wait_elevisble(Loginlocator.email_verification_code, doc)
        # self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code[cmdopt]['code'], doc)
        self.click_element(Loginlocator.email_submit_ele, doc)
        self.wait_elevisble(Loginlocator.admin_home_page, doc)
        self.driver.get(Commondata.home_page[cmdopt]['admin'])
        time.sleep(10)




    def login_out(self):
        doc='public logout portal'
        self.wait_elevisble(OrganizationLocator.loading_wait,doc)
        self.wait_eleinvisble(OrganizationLocator.loading_wait,doc)
        self.wait_elevisble(Loginlocator.personal_portrait,doc)
        time.sleep(3)
        self.move_mouse_to_click_element(Loginlocator.personal_portrait,doc)
        self.wait_elevisble(Loginlocator.logout,doc)
        self.click_element(Loginlocator.logout,doc)
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.refresh_page()
        time.sleep(5)

    def reset_pwd_login(self,email,cmdopt):
        doc='user new password login'
        self.refresh_page()
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code)
        self.keyboard_event(Loginlocator.login_username_ele,doc)
        self.keyboard_event(Loginlocator.login_password_ele,doc)
        self.send_keys(Loginlocator.login_username_ele, email,doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.reset_pwd,doc)
        self.keyboard_event_enter(Loginlocator.login_password_ele,doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        #self.click_element(Loginlocator.login_submit_ele,doc)
        self.wait_elevisble(Loginlocator.email_verification_code,doc)
        # self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code[cmdopt]['code'],doc)
        self.click_element(Loginlocator.email_submit_ele,doc)
        self.wait_elevisble(Loginlocator.dshboard_ele,doc)
        time.sleep(10)


    def login_main_account (self,cmdopt):
        doc='user admin account login'
        self.driver.get(Commondata.url[cmdopt]['user'])
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data[cmdopt]['user']['username'],doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.user_data[cmdopt]['user']['password'],doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele,doc)
        #self.wait_elevisble(Loginlocator.email_verification_code)
        # self.click_element(Loginlocator.send_code_ele)
        time.sleep(2)
        self.wait_elevisble(Loginlocator.email_verification_code,doc)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code[cmdopt]['code'],doc)
        self.click_element(Loginlocator.email_submit_ele,doc)
        self.wait_elevisble(Loginlocator.dshboard_ele,doc)
        self.driver.get(Commondata.home_page[cmdopt]['admin'])
        time.sleep(10)

    def admin_login(self,cmdopt):
        doc='admin portal login'
        self.driver.get(Commondata.url[cmdopt]['admin'])
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        time.sleep(2)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data[cmdopt]['admin']['username'],doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.user_data[cmdopt]['admin']['password'],doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele,doc)
        time.sleep(2)
        self.wait_elevisble(Loginlocator.email_verification_code,doc)
        # self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code[cmdopt]['code'],doc)
        time.sleep(2)
        self.click_element(Loginlocator.email_submit_ele,doc)
        self.wait_elevisble(Loginlocator.admin_home_page,doc)
        time.sleep(10)

    # 通过传入的账号密码进行登录
    def admin_login_by_email(self, user, pwd, cmdopt):
        doc='admin portal login'
        self.driver.get(Commondata.url[cmdopt]['admin'])
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, user, doc)
        self.send_keys(Loginlocator.login_password_ele, pwd, doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele,doc)
        time.sleep(2)
        # self.wait_elevisble(Loginlocator.email_verification_code,doc)
        # # self.click_element(Loginlocator.send_code_ele)
        # self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code[cmdopt]['code'],doc)
        # time.sleep(2)
        # self.click_element(Loginlocator.email_submit_ele,doc)
        # self.wait_elevisble(Loginlocator.admin_home_page,doc)
        time.sleep(10)

    #指定用户登录用户端
    def login_user(self,cmdopt,user):
        doc='login user admin'
        time.sleep(3)
        self.driver.get(Commondata.url[cmdopt]['user'])
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        #验证码
        #self.wait_elevisble(Loginlocator.verification_code,doc)
        self.send_keys(Loginlocator.login_username_ele,Logindata.user_data[cmdopt][user]['username'],doc)
        self.send_keys(Loginlocator.login_password_ele,Logindata.user_data[cmdopt][user]['password'],doc)
        #验证码
        #self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele,doc)
        # self.wait_elevisble(Loginlocator.email_verification_code,doc)
        #self.click_element(Loginlocator.send_code_ele)
        # self.send_keys(Loginlocator.email_verification_code,Logindata.verification_code[cmdopt]['code'],doc)
        # self.click_element(Loginlocator.email_submit_ele,doc)
        # self.wait_elevisble(Loginlocator.dshboard_ele,doc)
        time.sleep(2)
        login_success=self.get_element(Loginlocator.login_success_ele, doc).text
        time.sleep(10)
        return login_success

    def jump_to_adminportal(self,cmdopt):
        doc='会话内访问admin端'
        self.driver.get(Commondata.url[cmdopt]['admin'])
        self.wait_elevisble(Loginlocator.admin_home_page,doc)
        time.sleep(5)

    def jump_to_userportal(self,cmdopt):
        doc='会话内访问user端'
        self.driver.get(Commondata.url[cmdopt]['user'])
        self.wait_elevisble(Loginlocator.admin_home_page,doc)
        time.sleep(5)

    #指定用户登录管理员端
    def admin_login_user(self,cmdopt,user):
        doc='admin portal login'
        self.driver.get(Commondata.url[cmdopt]['admin'])
        time.sleep(5)
        self.wait_elevisble(Loginlocator.login_username_ele,doc)
        self.wait_elevisble(Loginlocator.login_password_ele,doc)
        # 验证码
        # self.wait_elevisble(Loginlocator.verification_code)
        time.sleep(3)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data[cmdopt][user]['username'],doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.user_data[cmdopt][user]['password'],doc)
        # 验证码
        # self.send_keys(Loginlocator.verification_code,Logindata.verification_code['code'])
        time.sleep(3)
        self.click_element(Loginlocator.login_submit_ele,doc)
        self.wait_elevisble(Loginlocator.email_verification_code,doc)
        # self.click_element(Loginlocator.send_code_ele)
        self.send_keys(Loginlocator.email_verification_code, Logindata.verification_code[cmdopt]['code'],doc)
        time.sleep(2)
        self.click_element(Loginlocator.email_submit_ele,doc)
        self.wait_elevisble(Loginlocator.admin_home_page,doc)
        time.sleep(10)